package com.ssy.lingxi.open.api.controller.product;

import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.component.rabbitMQ.constants.MessageConstants;
import com.ssy.lingxi.component.rabbitMQ.service.IRabbitMQUtils;
import com.ssy.lingxi.open.api.controller.ApiBaseController;
import com.ssy.lingxi.product.api.model.request.commodity.CommodityAddRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 商品管理类
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/10/13
 */
@RestController
@RequestMapping("/openApi/commodity")
public class CommodityController extends ApiBaseController {

    @Resource
    private IRabbitMQUtils rabbitMQUtils;

    /**
     * 批量同步商品
     * @param commodityRequestList 商品对象
     * @return 是否成功
     */
    @RequestMapping(value = "/syncBatchCommodity", method = RequestMethod.POST)
    public Wrapper<Boolean> syncBatchCommodity(@RequestBody @Valid List<CommodityAddRequest> commodityRequestList){
        //校验用户是否登录
        this.checkLogin();
        //将商品信息存入消息队列中
        commodityRequestList.forEach(commodityRequest -> rabbitMQUtils.sendMsg(MessageConstants.OPEN_API_COMMODITY_EXCHANGE, MessageConstants.OPEN_API_COMMODITY_ROUTINGKEY, JSONUtil.toJsonStr(commodityRequest)));
        return Wrapper.success(true);
    }


}
